[#ftl]
import {useMutation, useQuery, useResult} from "@vue/apollo-composable";
import gql from "graphql-tag";

export class ${entity.entityName}Service {
    search(first: number, offset: number, filter: {}): any {
        const {result, fetchMore, loading} = useQuery(
            gql`
                query ${entity.pName}_search ($first:Int,$offset:Float,$filter:${entity.entityName}Filter!) {
                    ${entity.pName}_search(first:$first,offset:$offset,filter:$filter){
                        uuid
                        resultInfo{
                            queryTime
                            sign
                            totalCount
                        }
                        edges{
                            id
                            [#list entity.fields as field]
                                ,${field.name}
                            [/#list]
                        }
                        pageInfo{
                            offset,size
                        }
                    }
                }
            `,
            () => ({first, offset, filter: {}})
            , {
                notifyOnNetworkStatusChange: true
            }
        );

        function loadMore(offset: number, filter: any) {
            fetchMore({
                variables: {
                    offset, filter
                },
                updateQuery: (prev, more) => {
                    return more.fetchMoreResult;
                },
            });
        }

        const datas = useResult(
            result,
            [],
            (data) => data.${entity.pName}_search.edges
        );
        const pageInfo = useResult(
            result,
            {
                offset: 0, size: 15
            },
            (data) => data.${entity.pName}_search.pageInfo
        );
        const totalCount = useResult(
            result,
            0,
            (data) => data.${entity.pName}_search.resultInfo.totalCount
        );
        return {datas, pageInfo, loadMore, loading, offset, first, totalCount};
    }

    [#list entity.datas as data]
        [#if data.list?ends_with("List")]
    ${data.list}() {
        const {result, loading} = useQuery(gql`
            query ${data.service}{
                    ${data.service}{
                    resultInfo{
                        totalCount
                        queryTime
                        sign
                    }
                    uuid
                    edges{
                        [#list data.fields as f][#if f_index!=0],[/#if]${f}[/#list]
                    }

                }
            }
        `);
        const datas = useResult(
            result,
            [],
            (data) => data.${data.service}.edges
        );
        return {${data.list}: datas, ${data.list}Loading: loading}
    }
    [#elseif data.list?ends_with("Enums")]
        ${data.list}(){
            return {${data.list}:[
                [#list data.datas as enum]
                [#if enum_index!=0],[/#if]{name:'${enum.name}',label:'${enum.label}'}
                [/#list]
            ]};
        }
    [/#if]
    [/#list]
    remove(): any {
        const {mutate: removeEntity, onDone: doneRemove} = useMutation(gql`
                    mutation ${entity.pName}_removeByIdList($id:[String]){
                        ${entity.pName}_removeByIdList(ids:$id) {
                            status
                            rows
                            messages
                            uuid
                            executeTime
                            sign
                        }
                    }
            `, () => ({
                fetchPolicy: 'no-cache'
            })
        );
        return {removeEntity, doneRemove};
    }

    save(): any {
        const {mutate: saveEntity, onDone: doneSave} = useMutation(gql`
                    mutation ${entity.pName}_save($model:${entity.entityName}Input!){
                        ${entity.pName}_save(model:$model){
                            messages,status,rows,uuid,sign
                        }
                    }
            `, () => ({
                fetchPolicy: 'no-cache'
            })
        );
        return {saveEntity, doneSave};
    }
}
